package com.example.wangzh.mytest;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.example.wangzh.mytest.base.BaseActivity;
import com.example.wangzh.mytest.databinding.ActivityPermissionTestBinding;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.observers.DisposableObserver;

public class PermissionTestActivity extends BaseActivity<ActivityPermissionTestBinding> {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding.btn.setOnClickListener(this::click);
        mBinding.btnMultiPermission.setOnClickListener(this::click);
    }

    @SuppressLint("NonConstantResourceId")
    void click(View v) {
        switch (v.getId()) {
            case R.id.btn:
                reQuestPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.btn_multi_permission:
                requestEachPermissions(Manifest.permission.READ_PHONE_STATE);
                break;
            default:
                break;
        }
    }

    //申请单个或者多个权限,不在乎是否不再询问和哪个权限申请失败，只要有一个失败就执行失败操作
    @SuppressWarnings("SameParameterValue")
    private void reQuestPermission(final String s) {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(s).subscribe(new DisposableObserver<Boolean>() {
            @Override
            public void onNext(Boolean granted) {
                if (granted) {
                    Log.i("permission", "已获取权限：" + s);
                } else {
                    Log.i("permission", "未获取权限：" + s);
                }
            }

            @Override
            public void onError(Throwable e) {
                dispose();
            }

            @Override
            public void onComplete() {
                dispose();
            }
        });
    }

    //申请多个权限，在乎是否不再询问和哪个权限申请失败
    private void requestEachPermissions(String... s) {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.requestEach(s).subscribe(new DisposableObserver<Permission>() {
            @SuppressWarnings("unused")
            @Override
            public void onNext(Permission permission) {
                int a = 0;
                /*
                  manifest里面没有申请该权限，
                  直接返回   permission.shouldShowRequestPermissionRationale=false permission.granted=false

                  manifest里面申请了该权限，没有在应用权限里面给予权限
                   弹出弹窗，不勾选禁止后不再提示，点击禁止按钮
                   返回permission.shouldShowRequestPermissionRationale=true  permission.granted=false
                   弹出弹窗，不勾选禁止后不再提示，点击始终允许按钮
                   返回permission.shouldShowRequestPermissionRationale=false  permission.granted=true
                   弹出弹窗，勾选禁止后不再提示，点击禁止按钮
                   返回permission.shouldShowRequestPermissionRationale=false  permission.granted=false
                   弹出弹窗，勾选禁止后不再提示，点击始终允许按钮
                   返回permission.shouldShowRequestPermissionRationale=false  permission.granted=true

                   manifest里面申请了权限，用户允许了该权限
                   返回permission.shouldShowRequestPermissionRationale=false  permission.granted=true

                   manifest里面申请了权限，用户禁止了该权限,禁止时选的未勾选禁止后不再提示
                   弹出弹窗，根据弹窗返回相应结果

                   manifest里面申请了权限，用户禁止了该权限,禁止时选的选禁止后不再提示
                   返回permission.shouldShowRequestPermissionRationale=false  permission.granted=false
                 */

                if (permission.granted) {
                    toast("用户给予>>>" + permission.name);
                    return;
                }
                if (permission.shouldShowRequestPermissionRationale) {
                    toast("用户不给权限，允许下次继续申请>>>" + permission.name);
                    return;
                }
                //这里其实至少有两种情况，一种是app根本没有申请该权限，一种是用户点击了不再显示弹窗
                toast("用户不允许显示申请弹窗>>>" + permission.name);
            }

            @Override
            public void onError(Throwable e) {
                dispose();
            }

            @Override
            public void onComplete() {
                dispose();
            }
        });
    }

    public void toast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }
}
